const Router = require('koa-router');
const routes = new Router();

const mysql = require("mysql");

const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "111111",
    database: "myuser"
})
connection.connect();

routes.post("/api/login", async (ctx, next) => {
    try {
        const { username, password } = ctx.request.body;
        const result = await new Promise((resolve, reject) => {
            connection.query(`select * from myuser where username="${username}" and password="${password}"`, function (error, results) {
                if (error) reject(error.message)
                resolve(results)
            })
        })
        if (result.length === 1) {
            ctx.response.body = {
                code: 1,
                msg: "登录成功"
            }
        } else {
            ctx.response.body = {
                code: 0,
                msg: "登录失败"
            }
        }
    } catch (error) {
        ctx.response.body = {
            code: 1,
            msg: error
        }
    }
})
routes.get("/api/list", async (ctx, next) => {
    try {
        const result = await new Promise((resolve, reject) => {
            connection.query("select * from myuser", function (error, results) {
                if (error) reject(error.message)
                resolve(results)
            })
        })
        ctx.response.body = {
            code: 1,
            data: result
        }
    } catch (error) {
        ctx.response.body = {
            code: 0,
            msg: error
        }
    }
})
//删除
routes.post("/api/delete", async (ctx, next) => {
    try {
        const { id } = ctx.request.body
        const res = await new Promise((resolve, reject) => {
            connection.query("delete from myuser where id=?", id, function (error, results) {
                if (error) reject(error.message)
                resolve(results)
            })
        })
        ctx.response.body = {
            code: 1,
            msg: '删除成功'
        }
    } catch (error) {
        ctx.response.body = {
            code: 0,
            msg: error
        }
    }
})
//修改
routes.post("/api/update", async (ctx, next) => {
    /*  const { id, name, password, phone, age, sex } = ctx.request.body;
     console.log(id, name, password, phone, age, sex) */
    try {
        const { id, name, password, phone, age, sex } = ctx.request.body;
        await new Promise((resolve, reject) => {
            connection.query('update myuser set name=?,password=?,phone=?,age=?,sex=? where id=?', [name, password, phone, age, sex, id], function (error, result) {
                if (error) reject(error.message)
                resolve(result)
            })
        })
        const res = await new Promise((resolve, reject) => {
            connection.query('select * from myuser', function (error, result) {
                if (error) reject(error.message)
                resolve(result)
            })
        })

        ctx.response.body = {
            code: 1,
            msg: "修改成功",
            data: res,
        }
    } catch (error) {
        ctx.response.body = {
            code: 0,
            msg: error
        }
    }
})
//添加组员信息
routes.post("/api/add", async (ctx, next) => {
    try {
        const { user } = ctx.request.body;
        await new Promise((resolve, reject) => {
            connection.query("insert into myuser set ?", { username: user.username, password: user.password, name: user.name, phone: user.phone, sex: user.sex, age: user.age, }, function (error, result) {
                if (error) reject(error)
                resolve(result)
            })
        })
        ctx.response.body = {
            code: 1,
            msg: "添加成功"
        }
    } catch (error) {
        ctx.response.body = {
            code: 0,
            msg: error.message
        }
    }
})
module.exports = routes